<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Repraesentiert eine Saison
 * Methoden, um Saisons aus der DB abzurufen, editieren und loeschen
 *
 * @author Lopo
 */
class Season {
    private $id = null;
    private $name = null;
    private $startdate = null;
    private $enddate = null;

	public function __construct($data = array()) {
        if (isset($data['season_id'])) $this->id = (int) $data['season_id'];
        if (isset($data['season_name'])) $this->name = $data['season_name'];
        if (isset($data['season_startdate'])) $this->startdate = $data['season_startdate'];
        if (isset($data['season_enddate'])) $this->enddate = $data['season_enddate'];
    }
	
	public static function getSeasonById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_seasons WHERE season_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Season($row);
    }
	
	public static function getActualSeason() {
		$db = new Database();

        $sql = "SELECT * FROM ff_seasons WHERE YEAR(DATE(season_startdate)) = YEAR(CURDATE()) LIMIT 1";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Season($row);
	}
	
	public static function getSeasonList() {
        $db = new Database();

        $sql = "SELECT * FROM ff_seasons";

        $stmt = $db->prepare( $sql );
        $stmt->execute();
        
        $list = array();
        while ($row = $stmt->fetch()) {
            $season = new Season($row);
            $list[] = $season;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;

        return (array("seasonList" => $list, "totalRows" => $totalRows[0]));
    }
	
    public function insert() {
        //if (!is_null($this->id)) {
        if ($this->id != 0) {
            trigger_error("Season::insert(): Es wird versucht eine neue Saison anzulegen, obwohl bereits die ID gesetzt wurde (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_seasons (season_name, season_startdate, season_enddate) VALUES (:name, :startdate, :enddate)";
		
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":startdate", $this->startdate, PDO::PARAM_STR);
        $stmt->bindValue(":enddate", $this->enddate, PDO::PARAM_STR);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
	
	public function update() {
        if (is_null($this->id)) {
            trigger_error("Season::update(): Es wird versucht eine Saison zu speichern, die keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_seasons SET season_name=:name, season_startdate=:startdate, season_enddate=:enddate WHERE season_id=:id";
		
        $stmt = $db->prepare($sql);
		$stmt->bindValue(":name", $this->name, PDO::PARAM_STR);
        $stmt->bindValue(":startdate", $this->startdate, PDO::PARAM_STR);
        $stmt->bindValue(":enddate", $this->enddate, PDO::PARAM_STR);
		$stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
	
	public function delete() {
        if (is_null($this->id)) {
            trigger_error("Season::delete(): Es wird versucht eine Saison zu loeschen, die keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_seasons WHERE season_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getStartdate() {
        return $this->startdate;
    }

    public function getEnddate() {
        return $this->enddate;
    }


    
    
}
?>
